/*
* Copyright (c) 2025 Huawei Technologies Co., Ltd.
* openUBMC is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*         http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
*/

#include "test_wx_common.h"

using namespace dev;

namespace drivers {
namespace pcie_nic_card {
namespace wx {
namespace test {

// ========================= 驱动注册功能测试 =========================

TEST_F(WxAbiTest, register_device_driver_success) {
    // 验证wx驱动注册成功，并返回正确数量的设备类型
    EXPECT_EQ(m_devices.size(), 2);

    // 验证包含网卡设备类型
    EXPECT_TRUE(m_devices.find("PCIeNicCard") != m_devices.end());

    // 验证包含网口设备类型
    EXPECT_TRUE(m_devices.find("NicPort") != m_devices.end());
}

TEST_F(WxAbiTest, device_driver_structure_validation) {
    // 验证每个设备驱动的ABI接口完整性
    for (const auto& [device_name, device_driver] : m_devices) {
        // 验证设备名称非空
        EXPECT_FALSE(std::string(device_driver->device_name).empty());

        // 验证构造函数指针有效
        EXPECT_TRUE(device_driver->ctor != nullptr);

        // 验证初始化函数指针有效
        EXPECT_TRUE(device_driver->init != nullptr);

        // 验证启动函数指针有效
        EXPECT_TRUE(device_driver->start != nullptr);

        // 验证停止函数指针有效
        EXPECT_TRUE(device_driver->stop != nullptr);
    }
}

// ========================= 错误处理测试 =========================

TEST_F(WxAbiTest, device_pcienicard_with_null_device) {
    // 验证PCIeNicCard设备对空指针参数的错误处理能力
    auto device_driver = m_devices["PCIeNicCard"];
    ASSERT_TRUE(device_driver != nullptr);

    // 测试：传入nullptr设备名称，应返回nullptr
    void* device = device_driver->ctor(&m_test_service, nullptr);
    EXPECT_EQ(device, nullptr);

    // 测试：传入nullptr服务指针，应返回nullptr
    device = device_driver->ctor(nullptr, "wx_card_null_params");
    EXPECT_EQ(device, nullptr);

    // 测试：init函数传入nullptr设备指针，应返回错误
    mc::mutable_dict csr;
    mc::dict conn = create_test_connector();
    status_t init_ret = device_driver->init(nullptr, &csr, &conn);
    EXPECT_EQ(init_ret, STATUS_ERROR);

    // 测试：start函数传入nullptr设备指针，应返回错误
    status_t start_ret = device_driver->start(nullptr);
    EXPECT_EQ(start_ret, STATUS_ERROR);

    // 测试：stop函数传入nullptr设备指针，应返回错误
    status_t stop_ret = device_driver->stop(nullptr);
    EXPECT_EQ(stop_ret, STATUS_ERROR);
}

TEST_F(WxAbiTest, device_networkport_with_null_params) {
    // 验证NicPort设备对空指针参数的错误处理能力
    auto device_driver = m_devices["NicPort"];
    ASSERT_TRUE(device_driver != nullptr);

    // 测试：传入nullptr设备名称，应返回nullptr
    void* device = device_driver->ctor(&m_test_service, nullptr);
    EXPECT_EQ(device, nullptr);

    // 测试：传入nullptr服务指针，应返回nullptr
    device = device_driver->ctor(nullptr, "wx_port_null_params");
    EXPECT_EQ(device, nullptr);

    // 测试：init函数传入nullptr设备指针，应返回错误
    mc::mutable_dict csr;
    mc::dict conn = create_test_connector();
    status_t init_ret = device_driver->init(nullptr, &csr, &conn);
    EXPECT_EQ(init_ret, STATUS_ERROR);

    // 测试：start函数传入nullptr设备指针，应返回错误
    status_t start_ret = device_driver->start(nullptr);
    EXPECT_EQ(start_ret, STATUS_ERROR);

    // 测试：stop函数传入nullptr设备指针，应返回错误
    status_t stop_ret = device_driver->stop(nullptr);
    EXPECT_EQ(stop_ret, STATUS_ERROR);
}

// ========================= 正常流程测试 =========================
// 注意：详细的设备创建、初始化、启动/停止和多实例测试在 test_wx_card.cpp 和 test_wx_port.cpp 中

} // namespace test
} // namespace wx
} // namespace pcie_nic_card
} // namespace drivers
// 添加反射元数据定义（必须在命名空间外）
MC_REFLECT(drivers::pcie_nic_card::wx::test::test_interface, (Request))
MC_REFLECT(drivers::pcie_nic_card::wx::test::test_object, ((m_test_interface, "bmc.kepler.IpmiCore")))
